package com.cango.student.algorithm;

import java.util.PriorityQueue;
import java.util.Queue;

public class RK {

    public static void main(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue(3);


        offer(queue, 3, 1);
        offer(queue, 3, 2);
        offer(queue, 3, 3);
        offer(queue, 3, 4);
        offer(queue, 3, 5);
        Integer i;
        while ((i = queue.poll()) != null) {
            System.out.println(i);
        }

    }

    public static void offer(Queue<Integer> queue, int k, Integer num) {
        if (queue.size() < k) {
            queue.offer(num);
        } else if (queue.peek() < num) {//如果堆顶元素 < 新数，则删除堆顶，加入新数入堆
            queue.poll();
            queue.offer(num);
        }
    }

}
